package com.tool.btn;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;

import javax.swing.SwingConstants;

import com.tool.PanelDisplay.PetPanelShow1;
import com.tool.time.Limit;
import com.tool.time.TimeLimit;
import com.tool.time.TimerUtil;
import org.come.Frame.SkillMsgJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.ExpAddMapJpanel;
import org.come.Jpanel.ZhuJpanel;
import org.come.bean.FightOperation;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;

import com.tool.PanelDisplay.PetPanelShow;
import com.tool.image.ImageMixDeal;
import com.tool.imagemonitor.FightingMonitor;
import come.tool.Fighting.FightingEvents;
import come.tool.Fighting.FightingMixDeal;
import come.tool.Fighting.Fightingimage;
import come.tool.handle.HandleState;

public class FightingBtn extends MoBanBtn {

    int btntype;

    public FightingBtn(String iconpath, int type, Color[] colors, Font font, String text, int btntype) {
        super(iconpath, type, colors);
        // TODO Auto-generated constructor stub
        this.setText(text);

        setFont(font);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.btntype = btntype;
    }


    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        // TODO Auto-generated method stub
        btnjt();
    }

    /** 判断属于哪个按钮点击调用不同的方法 */
    public void btnjt() {
        switch (btntype) {
        case 0:
            Btnzidong();
            break;
        case 1:
            Btnfashu();
            break;
        case 2:
            Btndaoju();
            break;
        case 3:
            Btnfangyu();
            break;
        case 4:
            Btnbaohu();
            break;
        case 5:
            Btnzhaohuan();
            break;
         case 55:
                Btnzhaohuan1();
                break;
        case 6:
            Btnzhaohui();
            break;
        case 7:
            Btnbuzhua();
            break;
        case 8:
            Btntaopao();
            break;
        case 9:
            Btnbaohu111();
            break;
        case 11:


            if (FightingMixDeal.State == 2 || FightingMixDeal.State == 3) {
                FightingEvents fightingEvents = FightingMonitor.CheHuiFightingEvents();

                if (FightingMixDeal.camp == -1)
                  return;
                fightingEvents.setFightingsum(FightingMixDeal.FightingNumber);
                fightingEvents.setCurrentRound(FightingMixDeal.CurrentRound);
                FightingMixDeal.State=HandleState.HANDLE_PLAYER;

                String sendMes = Agreement.getAgreement().battleroundAgreement(GsonUtil.getGsonUtil().getgson().toJson(fightingEvents));
                SendMessageUntil.toServer(sendMes);

                while (FightingMixDeal.State == HandleState.HANDLE_PLAY) {
        			FightingMixDeal.PalyProgress(36);
        		}
                FightingMixDeal.RoundDecision();
                ZhuFrame.getZhuJpanel().ShowManBtn(FightingMixDeal.isLL());
              }
              break;

        case 30034:
        	ExpAddMapJpanel.iWantToFlyGo(30034);;
            break;
        default:
            break;
        }
    }

    /** 自动点击 */
    public static void Btnzidong()
//    {
//        try {
//            if (ZhuFrame.getZhuJpanel().getCaozuo() == 0) {
//                        FightingMixDeal.zdhh = 9999;
//                        ZhuFrame.getZhuJpanel().setCaozuo(1);
//                        ZhuFrame.getZhuJpanel().getZidong().setIcons(CutButtonImage.cuts("inkImg/hongmu/1/ss528.png"));
//                        FormsManagement.showForm(634);
//                    }
//                    else {
//                        if (ZhuFrame.getZhuJpanel().getCaozuo() == 2) {
//                            final String[] teams = ImageMixDeal.userimg.getRoleShow().getTeam().split("\\|");
//                            if (teams[0].equals(ImageMixDeal.userimg.getRoleShow().getRolename())) {
//                                final String fightMes = Agreement.getAgreement().battleConnectionAgreement("-1");
//                                SendMessageUntil.toServer(fightMes);
//                            }
//                            else {
//                                ZhuFrame.getZhuJpanel().addPrompt2("队员没法主动离开观战");
//                            }
//                        }
//                    }
//
//                if (ZhuFrame.getZhuJpanel().getZidong().getText().equals("取消")) {
//                    ZhuFrame.getZhuJpanel().getZidong().setText("自动");
//                    FightingMixDeal.zdhh = 0;
//                    FormsManagement.HideForm(634);
//                }
//                else {
//                    if (ZhuFrame.getZhuJpanel().getZidong().getText().equals("自动")) {
//                        FightingMixDeal.zdhh = 9999;
//                        ZhuFrame.getZhuJpanel().getZidong().setText("取消");
//                        FormsManagement.showForm(634);
//                    }
//                    else {
//                        if (ZhuFrame.getZhuJpanel().getZidong().getText().equals("离开")) {
//                            final String[] teams = ImageMixDeal.userimg.getRoleShow().getTeam().split("\\|");
//                            if (teams[0].equals(ImageMixDeal.userimg.getRoleShow().getRolename())) {
//                                final String fightMes = Agreement.getAgreement().battleConnectionAgreement("-1");
//                                SendMessageUntil.toServer(fightMes);
//                            }
//                            else {
//                                ZhuFrame.getZhuJpanel().addPrompt2("队员没法主动离开观战");
//                            }
//                        }
//                    }
//                }
//
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    {
        if ("取消".equals(ZhuFrame.getZhuJpanel().getZidong().getText())) {
            ZhuFrame.getZhuJpanel().getZidong().setText("自动");
            FightingMixDeal.zdhh = 0;
            FormsManagement.HideForm(634);
        } else if ("自动".equals(ZhuFrame.getZhuJpanel().getZidong().getText())) {
            FightingMixDeal.zdhh = 9999;
            ZhuFrame.getZhuJpanel().getZidong().setText("取消");
            FormsManagement.showForm(634);
        } else if ("离开".equals(ZhuFrame.getZhuJpanel().getZidong().getText())) {
            String[] teams = ImageMixDeal.userimg.getRoleShow().getTeam().split("\\|");
            if (teams[0].equals(ImageMixDeal.userimg.getRoleShow().getRolename())) {
                String fightMes = Agreement.getAgreement().battleConnectionAgreement("-1");
                SendMessageUntil.toServer(fightMes);
            } else {
                ZhuFrame.getZhuJpanel().addPrompt2("队员没法主动离开观战");
            }
        }
    }

    /**
     * 法术点击
     */
    public static void Btnfashu() {
        try {
            if (!FormsManagement.getInternalForm(34).getFrame().isVisible()) {
                if (FightingMixDeal.State == 1 || FightingMixDeal.State == 2) {
                    final Fightingimage fightingimage = FightingMixDeal.getdata(FightingMixDeal.State - 1);
                    SkillMsgJframe.getSkillMsgJframe().getSkillMsgJpaenl().showSkill(fightingimage.getFightingManData().cxxx("技能"), FightingMixDeal.State);
                }
            }
            else {
                FormsManagement.HideForm(46);
                FormsManagement.HideForm(34);
                FormsManagement.HideForm(631);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 道具点击
     */
    public void Btndaoju() {
        if (!FormsManagement.getInternalForm(2).getFrame().isVisible()) {
            FormsManagement.showForm(2);
        } else {
            FormsManagement.HideForm(2);
        }
    }

    /** 防御点击 */
    public static void Btnfangyu() {
        FightOperation operation = FightingMonitor.getOperation();
        operation.Record(-1, -1, 5, null);
        FightingMonitor.execution(operation);
    }

    /** 保护点击 */
    public static void Btnbaohu() {
        FightingMonitor.mousesname = "保护";
        FightingMonitor.mousestate = 3;
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE8);
        ZhuFrame.getZhuJpanel().HideBeastBtn();
    }

    public static void Btnbaohu111() {
        ZhuFrame.getZhuJpanel().addPrompt2("月卡专属！！！");
        Limit limit = TimeLimit.getLimits().getLimit("VIP");

        if (limit == null) {
            // 如果没有VIP限制，直接返回
            return;
        }

        int residueDay = TimerUtil.residueDay(limit.getTime());
        if (residueDay < 0) {
            // 如果VIP已过期，直接返回
            return;
        }

        // 判断按钮状态是否为“认输”
        String buttonText = ZhuFrame.getZhuJpanel().getZidong111().getText();
        if ("认输".equals(buttonText)) {
            // 检查特殊战斗模式
            if (FightingMixDeal.BattleType == 5) {
                // PK模式
                ZhuFrame.getZhuJpanel().addPrompt2("PK模式下不能使用VIP认输功能");
                return;
            } else if (FightingMixDeal.BattleType == 11 || FightingMixDeal.BattleType == 12) {
                // 天梯模式
                ZhuFrame.getZhuJpanel().addPrompt2("天梯模式下不能使用VIP认输功能");
                return;
            } else if (FightingMixDeal.BattleType == 31) {
                // 帮战模式
                ZhuFrame.getZhuJpanel().addPrompt2("帮战模式下不能使用VIP认输功能");
                return;
            } else if (FightingMixDeal.BattleType == 33 || FightingMixDeal.BattleType == 34) {
                // 水陆大会
                ZhuFrame.getZhuJpanel().addPrompt2("水陆大会中不能使用VIP认输功能");
                return;
            }
            // 检查是否为组队情况
            String[] teams = ImageMixDeal.userimg.getRoleShow().getTeam().split("\\|");
            if (teams.length > 1) {
                // 组队情况下，检查是否为队长
                String roleName = ImageMixDeal.userimg.getRoleShow().getRolename();
                if (!teams[0].equals(roleName)) {
                    ZhuFrame.getZhuJpanel().addPrompt2("只有队长才能使用此功能");
                    return;
                }
            }

            // 播放音效
            Music.addyinxiao("逃跑成功.mp3");

            // 结束战斗
            FightingMixDeal.FightingEnd(null);

            // 发送消息给服务器，通知所有队员退出战斗
            String fightMes = Agreement.getAgreement().battleConnectionAgreement("-1");
            SendMessageUntil.toServer(fightMes);
        }
    }

    /** 召唤点击 */
    public static void Btnzhaohuan() {
        if (!FormsManagement.getframe(6).isVisible()) {
            if (FightingMixDeal.State == HandleState.HANDLE_PLAYER) {
                PetPanelShow.Show();
            }
        } else {
            FormsManagement.HideForm(6);
        }
    }

    /** 召唤点击 */
    public static void Btnzhaohuan1() {
        if (!FormsManagement.getframe(666).isVisible()) {
            if (FightingMixDeal.State == HandleState.HANDLE_PLAYER) {
                PetPanelShow1.Show();
            }
        } else {
            FormsManagement.HideForm(666);
        }
    }
    /** 召回点击 */
    public static void Btnzhaohui() {
        // 判断是否有召唤兽在
        if (FightingMixDeal.MyBeastLifeAndDeath()) {
            FightingMonitor.FightingOperation(SpellGenerate("召回"));
            if (FightingMixDeal.State == HandleState.HANDLE_PLAYER) {
                if (FightingMixDeal.MyBeastLifeAndDeath()) {
                    FightingMixDeal.changeState(HandleState.HANDLE_PET);
                    ZhuFrame.getZhuJpanel().HideBeastBtn();
                    ZhuFrame.getZhuJpanel().ShowBeastBtn();
                } else {
                    FightingMixDeal.changeState(HandleState.HANDLE_WAIT);
                    FightingMixDeal.RoundFighting();
                }
            }
        }
    }

    /**
     * 捕抓点击
     */
    public static void Btnbuzhua() {
        FightingMonitor.mousestate = 4;
        FightingMonitor.mousesname = "捕捉";
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE7);
        ZhuFrame.getZhuJpanel().HideBeastBtn();
    }

    /**
     * 逃跑点击
     */
    public static void Btntaopao() {
        FightOperation operation = FightingMonitor.getOperation();
        operation.Record(-1, -1, 6, null);
        FightingMonitor.execution(operation);
    }
    
    /**
     * 鼠标进入
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
    }
    /**
     * 鼠标离开
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
    }
    /**
     * 将攻击者和被攻击者位置传入 生成法术攻击数据 type 0是玩家 1是召唤兽
     */
    public static FightingEvents SpellGenerate(String type) {
        FightingEvents fEvents = new FightingEvents();
        if (FightingMixDeal.State == HandleState.HANDLE_PLAYER) {
            fEvents.setOriginator(FightingMixDeal.FightingState(type, 0));
        } else {
            fEvents.setOriginator(FightingMixDeal.FightingState(type, 1));
        }
        return fEvents;
    }
}
